import 'package:flutter/foundation.dart';

import './home.dart';
import './category.dart';
import './order.dart';
import './person.dart';
import 'package:flutter/material.dart';

class MyTabbar extends StatefulWidget {
  MyTabbar({Key key}) : super(key: key);

  _MyTabbarState createState() => _MyTabbarState();
}

class _MyTabbarState extends State<MyTabbar> {
  int _selectIndex = 1;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var list = [
      {'title': '首页', 'icon': Icons.home},
      {'title': '分类', 'icon': Icons.category},
      {'title': '', 'icon': null},
      {'title': '订单', 'icon': Icons.chat},
      {'title': '个人中心', 'icon': Icons.person},
    ];
    List<BottomNavigationBarItem> _buildTabbarItem(items) {
      List<BottomNavigationBarItem> list = new List<BottomNavigationBarItem>();
      int index = 0;
      for (var item in items) {
        print(item);
        if (index == 2) {
          list.add(BottomNavigationBarItem(
              icon: Icon(
                item['icon'],
                size: 0,
              ),
              title: Text('${item['title']}')));
        } else {
          list.add(BottomNavigationBarItem(
              backgroundColor: Theme.of(context).primaryColor,
              icon: Icon(
                item['icon'],
                size: 32,
              ),
              title: Text('${item['title']}')));
        }

        index++;
      }
      return list;
    }

    return Container(
      child: Scaffold(
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            elevation: 0,
            onPressed: () {},

            // shape: CircleBorder(),
          ),
          floatingActionButtonLocation:
              FloatingActionButtonLocation.centerDocked,
          bottomNavigationBar: BottomNavigationBar(
            items: _buildTabbarItem(list),
            type: BottomNavigationBarType.fixed,
            currentIndex: _selectIndex,

            // fixedColor: Theme.of(context).primaryColor,
            // backgroundColor: Colors.green,
            onTap: _itemTapped,
          ),
          body: [
            Home(),
            CategoryView(),
            null,
            OrderView(),
            PersonView()
          ][_selectIndex]),
    );
  }

  void _itemTapped(int index) {
    if (index == 2) return null;
    setState(() {
      _selectIndex = index;
    });
  }
}
